package com.pignest.hotel.ui.reception;

import java.util.List;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.definition.BookingStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;

public class BookingListbox extends Listbox{
	private static final long serialVersionUID = -3612312856706006041L;
	
	private ReceptionService receptionService;
	private Booking selectBooking;
	
	public BookingListbox(){
		receptionService =Services.getReceptionService();
	}
	
	public void onCreate() {
		query(null, null, BookingStatus.ONGOING);
	}
	
	public void query(String name,String phone,BookingStatus status){
		List <Booking> bookings =receptionService.queryBooking(name, phone, status);
		int i=0;
		for(final Booking b:bookings) {
			Listitem listitem = new Listitem();
			listitem.setValue(b.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(Integer.toString(i+1)));
			listitem.appendChild(new Listcell(b.getBookingStatus()));
			listitem.appendChild(new Listcell(b.getName()));
			listitem.appendChild(new Listcell(b.getPhone()));
			listitem.appendChild(new Listcell(b.getEstimatedTimeLabel()));
			listitem.appendChild(new Listcell(b.getBookingRoomNames()));
			Integer p=b.getPreserve();
			if(p!=null)
			   listitem.appendChild(new Listcell(String.valueOf(p/60)));
			else
				listitem.appendChild(new Listcell(""));
			listitem.appendChild(new Listcell(b.getEndTimeLabel()));
//			listitem.appendChild(new Listcell(DateUtil.toString(b.getLeaveTime())));
			listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				public void onEvent(Event event) throws Exception { 
					selectBooking=b;
				}
			});
			i++;
		}
	}

	public boolean cancelBooking() {
		if (selectBooking == null) {
			UIUtils.alert("没有选择预定！");
			return false;
		}
		if (!BookingStatus.ONGOING.is(selectBooking.getStatus())) {
			UIUtils.alert("该状态(非 预订中)无法取消");
			return false;
		}
		receptionService.cancelBooking(selectBooking);
		return true;
	}
	
	public boolean edit() {
		final Booking sBooking = getSelectBooking();
		if (sBooking == null) {
			UIUtils.alert("请选择一条记录！");
			return false;
		}
		if (!BookingStatus.ONGOING.is(sBooking.getStatus())) {
			UIUtils.alert("不能修改状态为'预订中'以外的预订, 请重新预订");
			return false;
		}
		EditBookingWindow win = (EditBookingWindow) Executions.createComponents(
				"/modules/booking/bookingModWindow.zul", null, null);
		win.setBooking(sBooking);
		try {
			win.doModal();
		} catch (Exception e) {
			return false;
		}
		return win.isSuccess();
	}

	public Booking getSelectBooking() {
		return selectBooking;
	}
}
